package flux;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.publisher.*;

import java.nio.file.ProviderMismatchException;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class FluxDemo01 {
    public static void main(String[] args) {
        Flux flux1=Flux.range(1,10);
        Flux.fromIterable(List.of(1,2,3));
        Flux.just(1,2,3,4);
       Flux<Long> flux2= Flux.create(sink->{
            for (int i = 0; i < 10; i++) {
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sink.next(System.currentTimeMillis());
            }
        });


       flux1.subscribe(new Subscriber() {
           private Subscription subscription;
           @Override
           public void onSubscribe(Subscription subscription) {
               System.out.println("开始消费。。。");
                this.subscription=subscription;
//                subscription.request(3);
           }

           @Override
           public void onNext(Object o) {
               System.out.println(o);
               subscription.request(1);
           }

           @Override
           public void onError(Throwable throwable) {

           }

           @Override
           public void onComplete() {
               System.out.println("结束消费。。。");
           }
       });

    }
    public static void main2(String[] args) {
        Mono<String> mono = Mono.create(sink -> {
            new Thread(){
                @Override
                public void run() {
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    sink.success("这是一条订单信息");
                }
            }.start();
        });

        work(mono);
    }
    private static  void work(Mono<String> mono){
        mono.subscribe(System.out::println);
        System.out.println("订阅开始，等。。。");
    }

}
